package com.hooker.plugins;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.hooker.base.BaseHooker;
import com.hooker.base.BaseXposedLoader;
import com.tool.hooker_inject.DisplayHooker;
import com.tool.hooker_inject.FileManager;

import org.json.JSONObject;

import java.io.File;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import ewb.rmvd.mc.SdkRunner;

public class InjectManager extends BaseXposedLoader {

    public InjectManager(Context context, XC_LoadPackage.LoadPackageParam loadParams) {
        super(context, loadParams);
    }

    @Override
    public void hook(Context context, XC_LoadPackage.LoadPackageParam loadParams) {
        super.hook(context, loadParams);
    }

    @Override
    public void hook(Object... calls) {

        BaseHooker.UnXposedHooker xposedHooker = new BaseHooker.UnXposedHooker(context,loadParams);
        xposedHooker.hook(calls);

        hookMethod(Application.class, "onCreate", new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
            }

            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);

                if("com.tool.hooker_inject".equals(loadParams.packageName)){
                    return;
                }

                Log.d("SDK","****start*****package="+loadParams.packageName);
                String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + ".omgtest" + File.separator + "inject.txt";

                String content = FileManager.readString(path);

                final Context context = (Context) param.thisObject;

                String aid = "cdate009";
                String cid = "cdate009";
                boolean straight = false;
                if (new File(path).exists() && !TextUtils.isEmpty(content)) {
                    JSONObject object = new JSONObject(content);

                    String aid1 = object.optString("aid");
                    if (!TextUtils.isEmpty(aid1))
                        aid = aid1;

                    String cid1 = object.optString("cid");
                    if (!TextUtils.isEmpty(cid1))
                        cid = aid1;

                    straight = object.getBoolean("qt");
                }
                Log.d("SDK", "aid :" + aid);
                Log.d("SDK", "cid :" + cid);
                Log.d("SDK","straight" + straight);
                if (!straight) {
                    Log.d("SDK","run jar");
                    com.depression.overtake.Insert.blouse(context, aid, cid);
                    Toast.makeText(context, "JAR包已加载", Toast.LENGTH_SHORT).show();
                }
                else{
                    Log.d("SDK","run straight");
                    DisplayHooker.refresh(context, 2280, 1080, 1F);
                    Toast.makeText(context, "强制替换已启动", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}
